package com.lake.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/test")
public class TestController {

	@RequestMapping(value = "/doTest")
	public ModelAndView doTest() {

		return new ModelAndView("test");
	}

	@ResponseBody
	@RequestMapping(value = "/access")
	public String access(String signature, String timestamp, String nonce, String token, String echostr) throws Exception{
		/*
		 * 规则描述
		 *1. 将token、timestamp、nonce三个参数进行字典序排序
		 *2. 将三个参数字符串拼接成一个字符串进行sha1加密
		 *3. 开发者获得加密后的字符串可与signature对比，标识该请求来源于微信 
		 */
		if(StringUtils.isEmpty(signature)){
			return "error:微信加密签名不能为空.";
		}
//		signature = "0f829ac4e6689efbd338abda4c090f90bab60725";
//		timestamp = "1455868453";
//		nonce = "1677866950";
//		token = "weixin";
//		?signature=0f829ac4e6689efbd338abda4c090f90bab60725&timestamp=1455868453&nonce=1677866950&token=weixin&echostr=cxy

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("timestamp", timestamp);
		map.put("nonce", nonce);
		map.put("token", token);
		System.out.println(Sha1.SHA1(map));
		if(signature.toUpperCase().equals(Sha1.SHA1(map))){
			System.out.println("则接入生效，成为开发者成功");
			return echostr;
		}else{
			return "error:接入失败.";
		}
	}
}
